package simpletestbed.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TextFileUtils {
	
	public TextFileUtils() {
		
	}
	
	public static void splitFile(String originalFileName, String destFileName, int startLine, int numLines) throws IOException {
		FileReader fReader = new FileReader(originalFileName);
		BufferedReader reader = new BufferedReader(fReader);
		FileWriter fWriter = new FileWriter(destFileName);
		BufferedWriter writer = new BufferedWriter(fWriter);
		String line = null;
		int lineNum = 0;
		while ((line = reader.readLine()) != null) {
			if (lineNum >= startLine) {
				if (lineNum >= startLine+numLines) {
					break;
				}
				writer.write(line);
				writer.write("\n");
			}
			lineNum++;
		}
		writer.close();
		fWriter.close();
		reader.close();
		fReader.close();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			if (args != null && args.length >= 1) {
				String command = args[0];
				if (command.equals("split")) {
					splitFile(args[1], args[2], Integer.parseInt(args[3]), Integer.parseInt(args[4]));
				}
			}
			else {
				splitFile("C:\\MyProjects\\LogMining\\data\\apache_error_users.log", "C:\\MyProjects\\LogMining\\data\\apache_error-100K.log", 0, 100*1000);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

}
